import O1HD from "../../engines/gameEngine";
import level_cardComposition from "../../levelBundles/level_cardComposition/level_cardComposition";
import { IInGamePagePrams } from "../Interface";
import InGamePage from "./InGamePage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class CardCompositionInGamePage extends InGamePage {

    @property({type:cc.Node,tooltip:"洗牌的手"})
    washHand:cc.Node = null;

    washFlag:boolean = false;

    level:level_cardComposition = null;

    @property({type:cc.Label,tooltip:"目标文本"})
    lbTarget:cc.Label = null;

    @property({type:cc.Node,tooltip:"titleNode"})
    titleNode:cc.Node = null;

    protected start(): void {
        this.scheduleOnce(()=>{
            cc.tween(this.titleNode).to(1,{opacity:0}).start();
        },3);   
    }

    bindLevel(level:level_cardComposition){
        this.level = level;
    }

    async OnPageOpen(prams: IInGamePagePrams) {
        super.OnPageOpen(prams);
        this.lbTarget.string = prams.title;
    }

    /**展示洗牌 */
    showWashCard(washCard:boolean){
        let hand:cc.Node = this.washHand.getChildByName("hand");
        let hand1:cc.Node = this.washHand.getChildByName("hand1");
        let card:cc.Node = this.washHand.getChildByName("card");
        switch(washCard){
            case true:{
                if(this.washFlag){
                    return;
                }
                this.washFlag = true;
                card.active = true;
                hand.active = true;
                hand1.active = true;

                cc.Tween.stopAllByTarget(card);
                cc.Tween.stopAllByTarget(hand);
                cc.Tween.stopAllByTarget(hand1);
                card.opacity = 255;
                hand.angle = -90;
                hand1.angle = -90;
                card.angle = -90;
                let needTime:number = 1.5;
                cc.tween(hand).to(needTime,{angle:0},{easing:cc.easing.smooth}).delay(1).to(needTime,{angle:-90},{easing:cc.easing.smooth}).call(()=>{
                    hand.active = false;
                }).start();
                cc.tween(hand1).to(needTime,{angle:0},{easing:cc.easing.smooth}).delay(1).to(needTime,{angle:-90},{easing:cc.easing.smooth}).call(()=>{
                    hand1.active = false;
                }).start();
                cc.tween(card).to(needTime,{angle:0},{easing:cc.easing.smooth}).start();
                break;
            }
            case false:{
                if(!this.washFlag){
                    return;
                }
                this.washFlag = false;
                cc.tween(card).to(1,{opacity:0}).call(()=>{
                    hand.active = false;
                    hand1.active = false;
                    card.active = false;
                }).start();
                break;
            }
        }
    }

    onBtnWashCard(){
        O1HD.getManagers().soundMgr.playSound("cardComposition/click");
        if(this.washFlag){
            if(O1HD.getServices().userSrv.getCoins() > 0){
                O1HD.getServices().userSrv.setCoins(-1,true);
                this.level.washCard();
            }else{
                O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                    this.level.washCard();
                },()=>{
                },null,"看广告洗牌");
            }   
        }
    }

    onBtnSynthesis(){
        this.level.onBtnSynthesis();
    }

    onBtnDealCard(){
        this.level.onBtnDealCard();
    }

}
